package hongcaosp.app.android.contact;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;

import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.BaseAdapter;
import hongcaosp.app.android.common.BaseHolder;
import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.contact.atme.AtmeHolder;
import hongcaosp.app.android.contact.atme.AtmePresenter;
import hongcaosp.app.android.modle.bean.IAtme;
import hongcaosp.app.android.contact.comment.CommentHolder;
import hongcaosp.app.android.contact.comment.CommentPresenter;
import hongcaosp.app.android.modle.bean.IMsgComment;
import hongcaosp.app.android.contact.fans.FansHolder;
import hongcaosp.app.android.contact.fans.FansPresenter;
import hongcaosp.app.android.modle.bean.IFans;
import hongcaosp.app.android.contact.good.GoodHolder;
import hongcaosp.app.android.contact.good.GoodPresenter;
import hongcaosp.app.android.modle.bean.IGooder;
import hongcaosp.app.android.modle.bean.Income;
import hongcaosp.app.android.modle.bean.InviteRecord;
import hongcaosp.app.android.user.settings.invite.InviteRecordHolder;
import hongcaosp.app.android.user.settings.invite.InviteRecordPresenter;
import hongcaosp.app.android.user.settings.wallet.holder.IncomeHolder;
import hongcaosp.app.android.user.settings.wallet.presenter.IncomePresenter;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.utils.StatusBarUtil;
import xlj.lib.android.base.view.OnRecycleItemClickListener;

public class ListActivity extends BaseActivity {

    public static final int LIST_TYPE_ATME = 3;
    public static final int LIST_TYPE_COMMENT = 4;
    public static final int LIST_TYPE_FANS = 1;
    public static final int LIST_TYPE_GOODER = 2;
    public static final int LIST_TYPE_RECHARGE = 5;   //钱包-充值记录
    public static final int LIST_TYPE_WITHDRAW = 6;   //钱包-提现记录
    public static final int LIST_TYPE_INVITE= 7;   //邀请有礼-记录

    private RecyclerView recyclerView;
    private MaterialRefreshLayout refreshLayout;
    private TextView titleTV;
    private BaseAdapter adapter;
    private ListPresenter listPresenter;
    private EmptyLayoutHelper emptyLayoutHelper;
    private int listType;

    @Override
    protected void initData(Intent intent) {
        listType = intent.getIntExtra("list_type",LIST_TYPE_ATME);
        switch (listType) {
            case LIST_TYPE_ATME:
                titleTV.setText("@我");
                emptyLayoutHelper.init("你还没有被@哦","赶快去和好友互动起来吧");
                adapter = new BaseAdapter<IAtme>() {
                    @NonNull
                    @Override
                    public AtmeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        return new AtmeHolder(getLayoutInflater().inflate(R.layout.item_atme,parent,false), ListActivity.this);
                    }
                };
                listPresenter = new AtmePresenter(new IListView<IAtme>() {
                    @Override
                    public void freshList(List<IAtme> data, boolean canLoadMore) {
                        setData(data,canLoadMore);
                    }

                    @Override
                    public void addList(List<IAtme> data, boolean canLoadMore) {
                        addData(data,canLoadMore);
                    }
                });
                break;
            case LIST_TYPE_COMMENT:
                titleTV.setText("评论");
                emptyLayoutHelper.init("还没人评论哦","赶快去上传作品吧～");
                adapter = new BaseAdapter<IMsgComment>() {
                    @NonNull
                    @Override
                    public BaseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        return new CommentHolder(getLayoutInflater().inflate(R.layout.item_message_comment,parent,false), ListActivity.this);
                    }
                };
                listPresenter = new CommentPresenter(new IListView<IMsgComment>() {
                    @Override
                    public void freshList(List<IMsgComment> data, boolean canLoadMore) {
                        setData(data,canLoadMore);
                    }

                    @Override
                    public void addList(List<IMsgComment> data, boolean canLoadMore) {
                        addData(data,canLoadMore);
                    }
                });
                break;
            case LIST_TYPE_FANS:
                titleTV.setText("粉丝");
                emptyLayoutHelper.init("你还没有粉丝哦","赶快去上传作品吧～");
                adapter = new BaseAdapter<IFans>() {
                    @NonNull
                    @Override
                    public FansHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        return new FansHolder(getLayoutInflater().inflate(R.layout.item_fans,parent,false), ListActivity.this);
                    }
                };
                listPresenter = new FansPresenter(new IListView<IFans>() {
                    @Override
                    public void freshList(List<IFans> data, boolean canLoadMore) {
                        setData(data,canLoadMore);
                    }

                    @Override
                    public void addList(List<IFans> data, boolean canLoadMore) {
                        addData(data,canLoadMore);
                    }
                });
                break;
            case LIST_TYPE_GOODER:
                titleTV.setText("赞");
                emptyLayoutHelper.init("你还没有被赞哦","赶快去上传作品吧～");
                adapter = new BaseAdapter<IGooder>() {
                    @NonNull
                    @Override
                    public GoodHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        return new GoodHolder(getLayoutInflater().inflate(R.layout.item_gooder,parent,false), ListActivity.this);
                    }
                };
                listPresenter = new GoodPresenter(new IListView<IGooder>() {
                    @Override
                    public void freshList(List<IGooder> data, boolean canLoadMore) {
                        setData(data,canLoadMore);
                    }

                    @Override
                    public void addList(List<IGooder> data, boolean canLoadMore) {
                        addData(data,canLoadMore);
                    }
                });
                break;
            case LIST_TYPE_RECHARGE:
                titleTV.setText("明细");
                emptyLayoutHelper.init("你还没有充值记录哦","赶快去充值吧～");
                adapter = new BaseAdapter<Income>() {
                    @NonNull
                    @Override
                    public BaseHolder<Income> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        return new IncomeHolder(getLayoutInflater().inflate(R.layout.item_income, parent, false), new OnRecycleItemClickListener() {
                            @Override
                            public void onItemClick(int position) {

                            }

                            @Override
                            public void onItemLongClick(int position) {

                            }
                        });
                    }
                };

                listPresenter = new IncomePresenter(new IListView<Income>() {
                    @Override
                    public void freshList(List<Income> data, boolean canLoadMore) {
                        setData(data,canLoadMore);
                    }

                    @Override
                    public void addList(List<Income> data, boolean canLoadMore) {
                        addData(data,canLoadMore);
                    }
                },1);
                break;
            case LIST_TYPE_WITHDRAW:
                titleTV.setText("明细");
                emptyLayoutHelper.init("你还没有提现记录哦","赶快去提现吧～");
                adapter = new BaseAdapter<Income>() {
                    @NonNull
                    @Override
                    public BaseHolder<Income> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        return new IncomeHolder(getLayoutInflater().inflate(R.layout.item_income, parent, false), new OnRecycleItemClickListener() {
                            @Override
                            public void onItemClick(int position) {

                            }

                            @Override
                            public void onItemLongClick(int position) {

                            }
                        });
                    }
                };

                listPresenter = new IncomePresenter(new IListView<Income>() {
                    @Override
                    public void freshList(List<Income> data, boolean canLoadMore) {
                        setData(data,canLoadMore);
                    }

                    @Override
                    public void addList(List<Income> data, boolean canLoadMore) {
                        addData(data,canLoadMore);
                    }
                },3);
                break;
            case LIST_TYPE_INVITE:
                titleTV.setText("邀请记录");
                emptyLayoutHelper.init("你还没有邀请记录哦～","赶快去邀请吧～");
                adapter = new BaseAdapter<InviteRecord>() {
                    @NonNull
                    @Override
                    public BaseHolder<InviteRecord> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                        return new InviteRecordHolder(getLayoutInflater().inflate(R.layout.item_invite_record, parent, false), new OnRecycleItemClickListener() {
                            @Override
                            public void onItemClick(int position) {

                            }

                            @Override
                            public void onItemLongClick(int position) {

                            }
                        });
                    }
                };

                listPresenter = new InviteRecordPresenter(new IListView<InviteRecord>() {
                    @Override
                    public void freshList(List<InviteRecord> data, boolean canLoadMore) {
                        setData(data,canLoadMore);
                    }

                    @Override
                    public void addList(List<InviteRecord> data, boolean canLoadMore) {
                        addData(data,canLoadMore);
                    }
                });
                break;
        }
        recyclerView.setAdapter(adapter);
        if (listPresenter != null) {
            listPresenter.freshData();
        }
    }

    private void setData(List data,boolean loadMore) {
        if (adapter != null) {
            adapter.setData(data);
            adapter.notifyDataSetChanged();
        }
        refreshLayout.finishRefresh();
        refreshLayout.setLoadMore(loadMore);
        if (data == null || data.size() == 0) {
            emptyLayoutHelper.show();
            refreshLayout.setLoadMore(false);
        } else {
            emptyLayoutHelper.hide();
        }
    }

    private void addData(List data,boolean loadMore) {
        if (adapter != null) {
            adapter.addData(data);
            adapter.notifyDataSetChanged();
        }
        refreshLayout.finishRefreshLoadMore();
        refreshLayout.setLoadMore(loadMore);
    }

    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarDarkTheme(this,false);

        recyclerView = findViewById(R.id.recycler_view);
        titleTV = findViewById(R.id.title);
        refreshLayout = findViewById(R.id.swipe_fresh);
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayout.VERTICAL,false));

        emptyLayoutHelper = new EmptyLayoutHelper(findViewById(R.id.empty_layout));

        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        refreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                if (listPresenter != null) {
                    listPresenter.freshData();
                }
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                if (listPresenter != null) {
                    listPresenter.loadMore();
                }
            }
        });

    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_list;
    }

    public static void toActivity(Context context,int listType) {
        if (context != null) {
            Intent intent = new Intent(context,ListActivity.class);
            intent.putExtra("list_type",listType);
            context.startActivity(intent);
        }
    }
}
